package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.service.IBrandService;
import com.leyou.item.pojo.Brand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    IBrandService brandService;

    @GetMapping("page")
    public ResponseEntity<PageResult<Brand>> queryBrandByPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy", required = false) String soryBy,
            @RequestParam(value = "desc", required = false) Boolean desc) {
        PageResult<Brand> pageResult = brandService.queryBrandByPage(key, page, rows, soryBy, desc);

        return ResponseEntity.ok(pageResult);
    }

    @PostMapping
    public ResponseEntity<Void> addBrand(Brand brand, @RequestParam("cids") Long[] ids) {

        brandService.addBrand(brand, ids);

        return ResponseEntity.status(HttpStatus.CREATED).build();
//        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 根据分类id查询品牌集合
     *
     * @param cid 分类id
     * @return
     */
    @GetMapping("cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandListByCid(@PathVariable("cid") Long cid) {
        return ResponseEntity.ok(brandService.queryBrandListByCid(cid));
    }

    /**
     * 根据品牌id查询品牌数据
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<Brand> queryBrandById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(brandService.queryBrandById(id));
    }

    @GetMapping("list")
    public ResponseEntity<List<Brand>> queryBrandByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(brandService.queryBrandByIds(ids));
    }
}
